dojo.provide("bunkai-extras.GoogleAjaxFeed"); 

dojo.require("dijit._Widget");
dojo.require("dijit._Templated");

dojo.declare(
        //The HelloWorld class we're declaring that's associated with the
        //sc namespace.
        "bunkai-extras.GoogleAjaxFeed",
    //dijit._Widget is the prototypical ancestor that provides important method
    //stubs like the ones below.
    //dijit._Templated is then mixed in and overrides dijit._Widget's
    //buildRendering method, which constructs the UI for the widget from
    //a template.
	// dijit._Widget, dijit._Templated, 
        [ dijit._Widget, dijit._Templated],
{
        //Path to the template of this widget. dijit._Templated uses this to
        //snatch the template from the named file via a synchronous call.
        templatePath: dojo.moduleUrl("bunkai-extras", "templates/GoogleAjaxFeed.html"),

	widgetsInTemplate: true, // Should magically enable dojo to let me have additional widgets in the template markup

	url: "", // rss feed url
	attributes: [], // TO just show these attributes on each feed item
	resultFormat: "",  // google.feeds.Feed.JSON_FORMAT, google.feeds.Feed.XML_FORMAT, or google.feeds.Feed.MIXED_FORMAT,
	onlyTopic: false, // DO not display anything if true, only send results as JSON objects by topic message bus
	topic: "/bunkai/google/ajaxfeed", // The topic used when sending JSON objects	
	feed: "", // private feed
	feedlist: "",

        //Any initialization code would go here in the constructor. dijit._Widget ande
	constructor : function() 
	{
		console.log("constructor for GoogleAjaxFeed called.");
		
	},

	postMixInProperties: function() 
	{
		// Main 
		console.log("postMixinProperties for GoogleAjaxFeed called.");	
		//this.id = this.domNode.id;
		console.log("this.id = '"+this.id+"'");
		this.feed = new google.feeds.Feed(this.url || "http://www.ajaxian.com/index.xml");
		var container = document.createElement('ul');
		container.style.linkType = "none";
		this.feedlist = container;
		
		/*
		var chs = this.domNode.childNodes;
		for(var i = 0; i < chs.lenght; i++)
		{
			var ch = chs[i];
			if 
		}
		*/
		this.feed.load(function(result) 
		{
			console.log("google feed callback result = "+result);
        		if (!result.error) 
			{
          			for (var i = 0; i < result.feed.entries.length; i++) 
				{
            				var entry = result.feed.entries[i];
            				var div = document.createElement("li");
					var a = document.createElement("a");
					a.href = entry.link;
					a.target = "_top";
           	 			a.appendChild(document.createTextNode(entry.title));
					div.appendChild(a);
					console.dir(entry);
            				container.appendChild(div);
          			}
        		}
			else
			{
				console.log("feed error: "+result.error);
			}
      		});
	},

	postCreate: function() 
	{		
		console.log("postcreate for GoogleAjaxFeed called.");
		
		
		this.inherited("postCreate", arguments);
	},

	startup: function() 
	{
		console.log("startup for GoogleAjaxFeed called.");
		this.domNode.appendChild(this.feedlist);
	}
   }
);
